package cn.dark.auth.core.spring;

import cn.hutool.core.collection.CollUtil;
import org.springframework.boot.env.YamlPropertySourceLoader;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertySourceFactory;

import java.io.IOException;
import java.util.List;

/**
 * @author dark
 * @date 2023-10-28
 */
public class YamlPropertySourceFactory implements PropertySourceFactory {

    @Override
    public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
        YamlPropertySourceLoader sourceLoader = new YamlPropertySourceLoader();
        List<PropertySource<?>> load = sourceLoader.load(name, resource.getResource());
        if (CollUtil.isEmpty(load)) {
            return null;
        }
        return load.get(0);
    }

}
